package com.ttn.student.controller;

import cn.hutool.core.util.StrUtil;
import com.ttn.common.core.entity.Result;
import com.ttn.student.config.MinioUtils;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@Slf4j
@RequestMapping("/file")
@RestController
public class FileController {
    @Resource
    private MinioUtils minioUtils;

    @SneakyThrows
    @PostMapping("/uploadFile/{bucketName}")
    @ResponseBody
    public Result uploadFile(@RequestParam("file") MultipartFile multipartFile, @PathVariable String bucketName) {

        log.info("uploadFile  ----   -----  start ");
        String imgPath = minioUtils.uploadFileSingle(bucketName, multipartFile);
        log.info("uploadFile  ----   -----  end {}", imgPath);
        if (StrUtil.isNotBlank(imgPath)) {
            imgPath = StrUtil.split(imgPath, "?").get(0);
        }
        return Result.success(imgPath);
    }

}
